#include "application.h"
#include <thread>
#include <i_component_container.h>


CApplication::CApplication() : m_running(false)
{

}

CApplication::~CApplication()
{

}

CApplication& CApplication::instance()
{
	static CApplication ins;
	return ins;
}

void CApplication::Run()
{
	if (m_running)
		return;

	IComponentFactoryCenterPtr component_center;
	component_center = CreateFactoryCenter();
	component_center->LoadDir("./modules");
	IComponentContainerPtr container = component_center->CreateContainer();
	container->Register(m_command);
	container->Start();

	if(m_command)
		m_command->GetCommandSignal("quit").connect(this, &CApplication::_OnQuit);

	m_running = true;
	while (m_running)
	{
		container->Update();
		std::this_thread::sleep_for(std::chrono::milliseconds(50));
	}

	container->Stop();
}

void CApplication::_OnQuit(const CCommandInfoPtr&)
{
	if(m_running)
		m_running = false;
}
